﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace BucaUIMvc
{
    public static class HtmlHelperExtension
    {
        public static ExtensionsFactory BucaUI(this HtmlHelper helper)
        {
            return ExtensionsFactory.Instance;
        }
    }
    public class ExtensionsFactory
    {
        private static ExtensionsFactory _instance;
        public static ExtensionsFactory Instance
        {
            get { return _instance ?? (_instance = new ExtensionsFactory()); }
        }
        public LinkButtonExtension LinkButton(Action<LinkButtonSettings> method)
        {
            return CreateExtension<LinkButtonExtension, LinkButtonSettings>(method);
        }
        internal static TE CreateExtension<TE, TS>(Action<TS> method)
            where TE : ExtensionBase
            where TS : SettingsBase, new()
        {
            var settings = Activator.CreateInstance<TS>();
            method(settings);
            return (TE)CreateExtension(typeof(TE), settings);
        }
        internal static ExtensionBase CreateExtension(Type type, SettingsBase settings)
        {
            return (ExtensionBase)Activator.CreateInstance(type, new object[] { settings });
        }
    }
    public abstract class ExtensionBase
    {
        protected virtual TagBuilder GetTopTag(string tag, string cssCls, SettingsBase settings)
        {
            var top = new TagBuilder(tag);
            top.AddCssClass(cssCls);
            top.GenerateId(settings.Id);
            return top;
        }


        /// <summary>
        /// Gets the inner tag.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <param name="cssCls">The CSS CLS.</param>
        /// <param name="settings">The settings.</param>
        /// <returns></returns>
        protected virtual TagBuilder GetInnerTag(string tag, string cssCls, SettingsBase settings)
        {
            var inner = new TagBuilder(tag);
            inner.GenerateId(settings.Id);
            if (!string.IsNullOrEmpty(cssCls))
            {
                inner.AddCssClass(cssCls);
            }
            return inner;
        }


        /// <summary>
        /// Gets the inner tag.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <param name="cssCls">The CSS CLS.</param>
        /// <param name="title">The title.</param>
        /// <param name="dataOption">The data option.</param>
        /// <param name="style">The style.</param>
        /// <returns></returns>
        protected virtual TagBuilder GetInnerTag(string tag, string cssCls, string title, string dataOption, string style)
        {
            var inner = new TagBuilder(tag);
            if (!string.IsNullOrEmpty(cssCls))
            {
                inner.AddCssClass(cssCls);
            }
            if (!string.IsNullOrEmpty(title))
            {
                inner.Attributes.Add("title", title);
            }

            if (!string.IsNullOrWhiteSpace(dataOption))
            {
                inner.Attributes.Add("data-options", dataOption);
            }
            if (!string.IsNullOrWhiteSpace(style))
            {
                inner.Attributes.Add("style", style);
            }

            return inner;
        }


        public abstract string GetScripts();
        public abstract MvcHtmlString GetHtml();
    }

    public abstract class SettingsBase
    {
        /// <value>
        /// The identifier.
        /// </value>
        public string Id { get; set; }

        /// <summary>
        /// Gets or sets the user identifier.
        /// </summary>
        /// <value>
        /// The user identifier.
        /// </value>
        public int UserId { get; set; }
    }
}
